#include "UnitCard.h"
#include "DataDefine.h"
#include "globalVariables.h"

int UnitCard::paintTexture(SDL_Texture*& texture)
{
	SDL_Rect picRect = { CARD_PIC_X, CARD_PIC_Y, CARD_PIC_WIDTH, CARD_PIC_HEIGHT };
	//base render
	SDL_Surface* base = SDL_CreateRGBSurfaceWithFormat(0, CARD_WIDTH, CARD_HEIGHT, 32, SDL_PIXELFORMAT_RGBA8888);
	SDL_BlitSurface(cardSurface, NULL, base, NULL);
	//card name render
	SDL_Surface* surface = TTF_RenderUNICODE_Blended(titleFont, (Uint16*)((*name).c_str()), colorBlack);
	SDL_Texture* tempTexture = SDL_CreateTextureFromSurface(renderer, surface);
	int texW = 0;
	int texH = 0;
	SDL_QueryTexture(tempTexture, NULL, NULL, &texW, &texH);
	SDL_Rect titlerect = { CARD_TITLE_X + CARD_GAP, CARD_TITLE_Y + (CARD_TITLE_HEIGHT - texH) / 2, texW, texH };
	SDL_BlitSurface(surface, NULL, base, &titlerect);

	SDL_FreeSurface(surface);
	SDL_DestroyTexture(tempTexture);
	//card description render
	int descCount = 0;
	for (wstring wstr : *description)
	{
		surface = TTF_RenderUNICODE_Blended(descFont, (Uint16*)(wstr.c_str()), colorBlack);
		tempTexture = SDL_CreateTextureFromSurface(renderer, surface);
		SDL_QueryTexture(tempTexture, NULL, NULL, &texW, &texH);
		SDL_Rect descrect = { CARD_DESC_X + CARD_GAP, CARD_DESC_Y + CARD_GAP + descCount * texH, texW, texH };
		SDL_BlitSurface(surface, NULL, base, &descrect);

		SDL_FreeSurface(surface);
		SDL_DestroyTexture(tempTexture);
		descCount++;
	}
	//card cost render
	surface = TTF_RenderUNICODE_Blended(costFont, (Uint16*)(to_string(cost).c_str()), colorBlack);
	tempTexture = SDL_CreateTextureFromSurface(renderer, surface);
	SDL_QueryTexture(tempTexture, NULL, NULL, &texW, &texH);
	SDL_Rect costrect = { CARD_COST_X + (CARD_COST_WIDTH - texW) / 2, CARD_COST_Y + (CARD_COST_HEIGHT - texH) / 2, texW, texH };
	SDL_BlitSurface(surface, NULL, base, &costrect);

	SDL_FreeSurface(surface);
	SDL_DestroyTexture(tempTexture);
	//card attribute render
	int attributeCount = 0;
	list<wstring> attributeText;
	attributeText.push_back(L"ATK");
	attributeText.push_back(to_wstring(attack));
	attributeText.push_back(L"HP");
	attributeText.push_back(to_wstring(maxHealth));
	attributeText.push_back(L"MV");
	attributeText.push_back(to_wstring(move));
	for (wstring wstr: attributeText)
	{
		surface = TTF_RenderUNICODE_Blended(attributeFont, (Uint16*)(wstr.c_str()), colorBlack);
		tempTexture = SDL_CreateTextureFromSurface(renderer, surface);
		SDL_QueryTexture(tempTexture, NULL, NULL, &texW, &texH);
		SDL_Rect attrrect = { CARD_ATTRIBUTE_X+(CARD_ATTRIBUTE_WIDTH-texW)/2, CARD_ATTRIBUTE_Y+CARD_GAP+texH*attributeCount, texW, texH };
		SDL_BlitSurface(surface, NULL, base, &attrrect);

		SDL_FreeSurface(surface);
		SDL_DestroyTexture(tempTexture);
		attributeCount++;
	}
	//card picture render
	SDL_BlitSurface(*picture, NULL, base, &picRect);
	//assign
	texture = SDL_CreateTextureFromSurface(renderer, base);
	//release
	SDL_FreeSurface(base);
	return 0;
}